package me.demo.base;

import lombok.extern.slf4j.Slf4j;
import me.base.utils.RandomUtil;
import me.boot.db.mybatis.propertites.BasicDataSourceProperties;
import me.demo.base.entity.SysDept;
import me.demo.base.entity.SysLog;
import me.demo.base.entity.SysTenant;
import me.demo.base.entity.SysUser;
import me.demo.base.mapper.SysDeptMapper;
import me.demo.base.mapper.SysLogMapper;
import me.demo.base.mapper.SysTenantMapper;
import me.demo.base.mapper.SysUserMapper;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@Slf4j

@SpringBootTest
public class BuildData {

    @Autowired
    SysTenantMapper tenantMapper;

    @Autowired
    SysUserMapper userMapper;

    @Autowired
    SysDeptMapper deptMapper;

    @Autowired
    SysLogMapper logMapper;

    @Test
    public void buildTenantData() {
        List<SysTenant> tenants = buildTenants();
        tenants.forEach(tenant -> System.out.println("### insert tenants: " + tenantMapper.insert(tenant)));

        tenants.forEach(tenant -> {
            List<SysDept> depts = buildDepts(tenant);
            depts.forEach(dept -> System.out.println("### insert depts: " + deptMapper.insert(dept)));

            List<SysUser> users = buildUsers(tenant, depts.size());
            users.forEach(user -> System.out.println("### insert users: " + userMapper.insert(user)));

            List<SysLog> logs = buildLogs(tenant);
            logs.forEach(log -> System.out.println("### insert logs: " + logMapper.insert(log)));
        });
    }

    @Test
    public void genPropertyTest() {
        BasicDataSourceProperties p = genProperty(1);
        System.out.println(p.toString());
    }

    List<SysTenant> buildTenants() {
        List<SysTenant> foos = Lists.newArrayList();
        for (int i = 1; i <= 3; i++) {
            BasicDataSourceProperties p = genProperty(i);

            SysTenant po = new SysTenant();
            po.setName("租户_" + i);
            po.setSubDomain("w" + i);
            po.setDsConfig(p.toString());
            foos.add(po);
        }
        return foos;
    }

    BasicDataSourceProperties genProperty(int tenantId) {
        BasicDataSourceProperties p = new BasicDataSourceProperties();
        p.setDriverClassName("com.mysql.cj.jdbc.Driver");
        p.setUrl(String.format("jdbc:mysql://localhost:3306/tenant_%s", tenantId));
        p.setUsername("root");
        p.setPassword("123456");
        return p;
    }

    List<SysUser> buildUsers(SysTenant tenant, int deptCount) {
        List<SysUser> foos = Lists.newArrayList();
        for (int i = 1; i <= 20; i++) {
            SysUser po = new SysUser();
            po.setTenantId(tenant.getId());
            po.setDeptId((long) RandomUtil.getRandom(1, deptCount + 1));
            po.setName(tenant.getName() + "_用户_" + i);
            po.setPhone(RandomUtil.getRandomNumbers(8));
            foos.add(po);
        }
        return foos;
    }

    List<SysDept> buildDepts(SysTenant tenant) {
        List<SysDept> foos = Lists.newArrayList();
        for (int i = 1; i <= 5; i++) {
            SysDept po = new SysDept();
            po.setTenantId(tenant.getId());
            po.setName(tenant.getName() + "_部门_" + i);
            foos.add(po);
        }
        return foos;
    }

    List<SysLog> buildLogs(SysTenant tenant) {
        List<SysLog> foos = Lists.newArrayList();
        for (int i = 1; i <= 5; i++) {
            SysLog po = new SysLog();
            po.setTenantId(tenant.getId());
            po.setSource(RandomUtil.getRandomLowerCaseLetters(5));
            po.setContent(RandomUtil.getRandomLetters(20));
            foos.add(po);
        }
        return foos;
    }

}